package com.sdtv.mediunfusion.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.sdtv.basemodule.base.BaseActivity;
import com.sdtv.basemodule.router.ARouterUtils;
import com.sdtv.basemodule.utils.ClickProxy;
import com.sdtv.mediunfusion.R;
import com.sdtv.mediunfusion.utils.RouterPath;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.mediunfusion.widget
 * @ClassName: CustomeToolBar
 * @Description: 自定义toolbar
 * 里面封装了toolbar的基本用法
 * @Author: 小郭同学
 * @CreateDate: 2019-07-23 15:29
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class CustomeToolBar extends LinearLayout {

    @BindView(R.id.tb_back)
    ImageView tbBack;
    @BindView(R.id.tb_titleTextView)
    TextView titleView;
    @BindView(R.id.tb_phoneImg)
    ImageView tbPhonehImg;
    @BindView(R.id.appToolBarLayout)
    Toolbar toolBar;
    @BindView(R.id.tb_loggingText)
    TextView tbLogText;
    private Context context;

    public TextView getTbLogText() {
        return tbLogText;
    }

    public void setTbLogText(TextView tbLogText) {
        this.tbLogText = tbLogText;
    }

    /**
     * 点击事件
     */
    private ClickProxy clickProxy = new ClickProxy(new OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tb_back:
                    ((BaseActivity) context).onBackPressed();
                    break;
                case R.id.tb_phoneImg:
                    //跳转到电话记录页面 ---
                    ARouterUtils.navigation(RouterPath.TO_MELTCALL_LIST_ACTIVITY);
                    break;
                case R.id.tb_loggingText:
                    // 跳转到-打卡记录页面
                    ARouterUtils.navigation(RouterPath.TO_PUNCHLOGGING_ACTIVITY);
                    break;
                default:
            }
        }
    });

    public CustomeToolBar(Context context) {
        this(context, null);
    }

    public CustomeToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomeToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView(attrs);
    }

    /**
     * 初始化布局文件
     */
    private void initView(AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.toolbar_layout, this);
        ButterKnife.bind(this);
        //设置自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomeToolBar);
        int count = typedArray.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.CustomeToolBar_showBack:
                    // 获取xml中配置的值
                    showBackImage(typedArray.getBoolean(attr, true));
                    break;
                case R.styleable.CustomeToolBar_showPhone:
                    showPhoneImg(typedArray.getBoolean(attr, true));
                    break;
                case R.styleable.CustomeToolBar_titleText:
                    setTitle(typedArray.getString(attr));
                    break;
                case R.styleable.CustomeToolBar_rightText:
                    setRightText(typedArray.getString(attr));
                    break;
                case R.styleable.CustomeToolBar_showRight:
                    // 获取xml中配置的值
                    showRightTab(typedArray.getBoolean(attr, true));
                    break;
                default:
            }
        }
        // 绑定点击事件
        tbBack.setOnClickListener(clickProxy);
        tbPhonehImg.setOnClickListener(clickProxy);
        tbLogText.setOnClickListener(clickProxy);
    }

    /**
     * 设置标题名称
     *
     * @param title
     */
    public void setTitle(String title) {
        if (null != titleView && !TextUtils.isEmpty(title)) {
            titleView.setText(title);
        }
    }

    /**
     * 设置右侧文字标签内容
     *
     * @param text
     */
    public void setRightText(String text) {
        if (null != tbLogText && !TextUtils.isEmpty(text)) {
            tbLogText.setText(text);
        }
    }

    /**
     * 是否显示最右侧按钮
     *
     * @param isShow
     */
    public void showRightTab(boolean isShow) {
        if (isShow) {
            tbLogText.setVisibility(View.VISIBLE);
        } else {
            tbLogText.setVisibility(View.GONE);
        }
    }

    /**
     * 显示返回按钮  并绑定点击事件
     *
     * @param isShow
     */
    public void showBackImage(boolean isShow) {
        if (isShow) {
            tbBack.setVisibility(View.VISIBLE);
        } else {
            tbBack.setVisibility(View.GONE);
        }
    }

    /**
     * 显示搜索按钮
     *
     * @param isShow
     */
    public void showPhoneImg(boolean isShow) {
        if (isShow) {
            tbPhonehImg.setVisibility(View.VISIBLE);
        } else {
            tbPhonehImg.setVisibility(View.GONE);
        }

    }

}
